package com.intct;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import java.io.IOException;

/**
 * ----------------------------------
 * DDL:
 * 1.判断表是否存在
 * 2.创建表
 * 3.创建命名空间
 * 4.删除表
 * ----------------------------------
 * DML:
 * 1.插入数据(put)
 * 2、查找数据(get)
 * 3、查找全部数据(scan)
 * 4.删除数据(delete)
 * ----------------------------------
 */
public class HBaseTestApi {

    private static Connection connection = null;
    private static Admin admin = null;

    static {
        // 2.获取管理员对象
        try {
            // 1.获取配置文件信息
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "node1,node2,node3:2181");

            // 2. 创建连接对象
            Connection connection = ConnectionFactory.createConnection(configuration);

            // 3.创建Admin对象
            admin = connection.getAdmin();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 1.判断表是否存在
     * @param tableName
     * @return
     * @throws IOException
     */
    public static boolean isTableExist(String tableName) throws IOException {
        // 1.获取配置文件信息
        // Configuration configuration = HBaseConfiguration.create();
        // configuration.set("hbase.zookeeper.quorum", "node1,node2,node3:2181");

        // 2.获取管理员对象
        // Connection connection = ConnectionFactory.createConnection(configuration);
        // Admin admin = connection.getAdmin();

        // 3.判断表是否存
        boolean exists = admin.tableExists(TableName.valueOf(tableName));

        // 4.关闭连接
        // admin.close();

        // 5.返回结果
        return exists;
    }

    /**
     * 2.创建表
     * @param tableName
     * @param args
     */
    public static void createTable(String tableName, String... args) throws IOException {
        // 1. 判断是否存在列族信息
        if (args.length <= 0) {
            System.out.println("请设置列族信息！");
            return;
        }

        // 2.判断表是否存在
        if (isTableExist(tableName)) {
            System.out.println(tableName + "表已存在！");
            return;
        }

        // 3.创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

        // 4. 循环添加列族信息
        for (String arg : args) {
            // 5. 创建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(arg);

            // 6.添加具体列方向信息
            hTableDescriptor.addFamily(hColumnDescriptor);
        }

        // 7.创建表
        admin.createTable(hTableDescriptor);
    }

    /**
     * 关闭连接
     */
    public static void close() {
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 入口
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // 1.  测试表是否存在
        System.out.println(isTableExist("stu1"));

        // 2.  创建表测试
        createTable("stu1", "info01", "info02");

        System.out.println(isTableExist("stu1"));

        // 关闭资源
        close();
    }
}
